
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module daemon
* @author: Hjava
* @description: 发送心跳包保持长连接模块，登录成功后初始化
* @since: 2018-07-16 11:30:41
*/
var constant_1 = __webpack_require__(/*! _login/constant */ "./src/core/login/constant.ts");
var session_1 = __webpack_require__(/*! _login/session */ "./src/core/login/session.ts");
var constant_2 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var timeoutTry_1 = __webpack_require__(/*! _util/timeoutTry */ "./src/core/utils/timeoutTry.ts");
var userData_1 = __webpack_require__(/*! _util/userData */ "./src/core/utils/userData.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var constant_3 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
// 发送 ping 间隔时间
var SEND_PING_TIME = 10 * 1000;
// ping 等待超时时间，单位是秒
var PING_TIMEOUT_TIME = 10;
var Daemon = /** @class */ (function () {
    function Daemon(connection) {
        var _this = this;
        this._pingTry = null;
        this._connection = null;
        this._sendPingHandle = null;
        this.timeoutHandlerQuickPing = null;
        this._connection = connection;
        this._pingTry = new timeoutTry_1.default([PING_TIMEOUT_TIME], this);
        this._pingTry.timeout(function () {
            log_1.default.log('Daemon::constructor pingTimeout disconnect link');
            _this._connection.disconnect('pingTimeout');
        });
        // 当收到此数据包时,说明服务端没有接收到客户端的 Ping 包.此时需要立刻发送一个 DetectAck 包,否则服务端会断开当前连接
        eventEmitter_1.default.on(constant_1.default.Login.PDETECTCLIENT_URI, function (res) {
            _this._sendDetectAck(res.server_tsms);
        });
        eventEmitter_1.default.on(constant_3.LOGIN_EVENT.AUTH_SUCCESS, function () {
            clearInterval(_this._sendPingHandle);
            _this.resetTimeoutTimer();
            if (_this._connection.isWebSocket()) {
                _this._sendPingHandle = setInterval(function () {
                    _this._sendPing();
                }, SEND_PING_TIME);
            }
        });
        // 当消息通道收到协议时,认为通道可靠
        eventEmitter_1.default.on('_dataRecv', function () {
            _this.resetTimeoutTimer();
        });
        eventEmitter_1.default.on(constant_2.LOGIN_STATUS_CHANGE_EVENT, function (status) {
            if (status === constant_2.LOGIN_STATUS.DISCONNECTED) {
                clearInterval(_this._sendPingHandle);
                _this._sendPingHandle = null;
                // todo clear pingTry
                _this.resetTimeoutTimer();
            }
        });
    }
    Daemon.prototype.resetTimeoutTimer = function () {
        this._pingTry.reset();
        this.clearQuickPingTimeout();
    };
    Daemon.prototype._sendPing = function () {
        var _this = this;
        if (!this._connection.isWebSocket()) {
            return;
        }
        // Log.log('Daemon::_sendPing info:' + userData.get('uid'))
        this._connection.send(new session_1.default.Ping({
            'uid': userData_1.default.get('uid')
        }));
        this._pingTry.run(function () {
            _this._sendPing();
        });
    };
    Daemon.prototype.quickPing = function () {
        var _this = this;
        if (!this._connection.isWebSocket()) {
            return;
        }
        this.clearQuickPingTimeout();
        log_1.default.log('Daemon::_quickPing info:' + userData_1.default.get('uid'));
        this._connection.send(new session_1.default.Ping({
            'uid': userData_1.default.get('uid')
        }));
        if (!this.timeoutHandlerQuickPing) {
            this.timeoutHandlerQuickPing = setTimeout(function () {
                _this.clearQuickPingTimeout();
                log_1.default.log('Daemon::_quickPing timeout info:' + userData_1.default.get('uid'));
                _this._connection.disconnect('quickPingTimeout');
            }, 3000);
        }
    };
    Daemon.prototype._sendDetectAck = function (serverTsms) {
        log_1.default.log('Daemon::_sendDetectAck info:' + userData_1.default.get('uid'));
        this._connection.send(new session_1.default.DetectAck({
            uid: userData_1.default.get('uid'),
            server_tsms: serverTsms
        }));
    };
    Daemon.prototype.disconnect = function () {
        this._connection.disconnect(500);
    };
    Daemon.prototype.clearQuickPingTimeout = function () {
        if (this.timeoutHandlerQuickPing) {
            clearTimeout(this.timeoutHandlerQuickPing);
            this.timeoutHandlerQuickPing = null;
        }
    };
    return Daemon;
}());
exports.default = Daemon;


//# sourceURL=webpack://MTDX/./src/core/login/daemon.ts?